package com.manage.messagecollection;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.NamingException;

import com.framework.Dao;
import com.util.DBUtil;
import com.util.DateTools;
import com.util.Parameters;

public class CarDriverInformitionDao extends Dao{
	public CarDriverInformitionVo add(CarDriverInformitionVo vo){
		String sql = "insert into car_driver(id, car_id, driver_id)" +
		"	VALUES " +
		"	(?,?,?)";
		try {
			Parameters param = DBUtil.getParameters();
			param.setString(1, vo.getId());
			param.setString(2, vo.getCar_id());
			param.setString(3, vo.getDriver_id());
			if(0!=DBUtil.update(param, sql)){
				return vo;
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public CarDriverInformitionVo modify(CarDriverInformitionVo vo) {
		Parameters param = DBUtil.getParameters();
		String setSql = this.makeSet(param,vo);
		if(setSql == null || "".equals(setSql))
			return null;
		
		String sql = "UPDATE car_driver " + setSql +
			"	WHERE id=? ";
		
		try {
			param.setString(1, vo.getId());
			if ( 0!=DBUtil.update(param, sql) ) {
				return vo;
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public CarDriverInformitionVo findById(String id) {
		String sql  = "SELECT id, car_id, driver_id " +
				"FROM car_driver " +
				"where id =? ";
		try {
			Parameters param = DBUtil.getParameters();
			param.setString(1, id);
			return DBUtil.querySingular(param,sql, new CarDriverInformitionVo());
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}
		return null;
	}
}
